$(function () {
    var $datagrid = $("#datagrid"), $toolbar = $("#toolbar"), $orderType = $("#orderType"), $orderStatus = $("#orderStatus");
    $("#orderType").combobox({
        onChange: function (oldV, newV) {
            $datagrid.datagrid("reload");
            return newV;
        }
    });
    $("#orderStatus").combobox({
        onChange: function (oldV, newV) {
            $datagrid.datagrid("reload");
        }
    });
    $(".easyui-datetimebox").combo({
        onChange: function (newV, oldV) {
            doSearch();
        }
    })
    $datagrid.datagrid({
        singleSelect: true,
        fit: true,
        border: false,
        fitColumns: true,
        url: Constant.ctx("/mulanweb/admin/order/search"),
        method: 'post',
        pagination: true,
        toolbar: $toolbar,
        columns: [[
            {field: 'Id', title: '编号'},
            {field: 'OrderNumber', title: '订单号'},
            {field: 'WechatNickname', title: '昵称'},
            {field: 'Amount', title: '数量'},
            {field: 'UnitPrice', title: '付款时单价'},
            {field: 'totalPrice', title: '应付金额',formatter:function(v,r,i){
                return r.UnitPrice * r.Amount;
            }},
            {field: 'LastPrice', title: '实付金额',formatter:function(v,r,i){
                if(v == r.Target.Price * r.Amount){
                    return v;
                }else {
                    return "<span style='color:red'>"+v+"</span>";
                }
            }},
            {field: 'MobileNumber', title: '手机号码', formatter: function (v, r, i) {
                if (v) {
                    return v;
                } else {
                    return "未填写";
                }
            }},
            {
                field: 'StatusId', title: '状态', formatter: function (v, r, i) {
                if (v == 4) {
                    return '已支付';
                } else if (v == 5) {
                    return '未支付';
                } else if (v == 6) {
                    return '已过期';
                } else if (v == 7) {
                    return '已验证';
                } else if (v == 8) {
                    return '已评论';
                } else if (v == 9) {
                    return '已退款';
                } else {
                    return v;
                }

            }
            },
            {field: 'CreateTime', title: '下单时间'},
            {field: 'VerifyTime', title: '兑换时间'}
        ]],
        onBeforeLoad: function (param) {
            param.PageNumber = param.page;
            param.PageSize = param.rows;
            param.typeId = 23;
            var orderType = $orderType.combo("getValue");
            if (orderType && orderType != 0) {
                param.targetTypeId = orderType;
            }

            var couponCode = $("#couponCode").val();
            if (couponCode) {
                param.couponCode = couponCode;
            }

            var orderStatus = $("#orderStatus").combo("getValue");
            if (orderStatus && orderStatus != 0) {
                param.statusId = orderStatus;
            }

            var orderNumber = $("#orderNumber").val();
            if (orderNumber) {
                param.orderNumber = orderNumber;
            }

            var mobile = $("#mobile").val();
            if (mobile) {
                param.mobileNumber = mobile;
            }
            var nickname = $("#nickname").val();
            if (nickname) {
                param.wechatNickname = nickname;
            }

            var startVerifyTime = $("#startVerifyTime").combo("getValue");
            if (startVerifyTime) {
                param.startVerifyTime = startVerifyTime;
            }

            var endVerifyTime = $("#endVerifyTime").combo("getValue");
            if (endVerifyTime) {
                param.endVerifyTime = endVerifyTime;
            }

            return true;
        },
        loadFilter: function (data) {
            var newData = {};
            if (data.ReturnCode == 1) {
                newData.rows = data.DataList;
                if (newData.rows.length > 0) {
                    newData.total = data.TotalRow;
                } else {
                    newData.total = 0;
                }
            }
            return newData;
        },
        onDblClickRow: function (index, rows) {
            showDetail(rows.Id);
        }
    });
    function showDetail(orderId) {
        if (orderId) {
            $.post(Constant.ctx("/mulanweb/common/order/getDetail"), {orderId: orderId},function(info){
                var module = $("#detail").html();
                var compileModule = Template7.compile(module);
                var resultModule = compileModule(info);
                $("#dialogDetail").html(resultModule);
            })
            $("#dialogDetail").dialog({
                title: '详情',
                width: 400,
                height: 500,
                onClose:function(){
                    $("#dialogDetail").html("<h1>请重试</h1>");
                }
            });
        } else {
            Constant.showMessage("请选择对象");
        }
    }
})

function doSearch() {
    $("#datagrid").datagrid("reload");
}